const upng = require('./libs/UPNG.js');

/**
 * 摄像头帧图片数据转成Base64（UPNG插件效率更高）
 */
function cameraFrameToBase64(frame) {
  const data = new Uint8ClampedArray(frame.data);
  let uint8Array = upng.encode([frame.data], frame.width, frame.height, 16, 0);
  return uint8ArrayToBase64(uint8Array);
}

/**
 * Uint8Array图片数据转成Base64
 */
function uint8ArrayToBase64(uint8Array) {
  return wx.arrayBufferToBase64(uint8Array);
}

/**
 * Base64图片转成Uint8Array
 */
function base64ToUint8Array(base64) {
  return wx.base64ToArrayBuffer(base64);
}

/**
 * 修改图片尺寸
 */
function resizeImg(pixels, width, height) {
  /*
  let canvas = wx.createCanvasContext('tempCanvas2');
  canvas.width = width;
  canvas.height = height;
  canvas.drawImage(pixels, 0, 0, width, height)
  canvas.draw();
  setTimeout(function () {
    wx.canvasToTempFilePath({
      canvasId: 'tempCanvas2',
      success: function (res) {
        console.log(res.tempFilePath)
      },
      fail: function (error) {
        console.log(error)
      }
    })
  }, 100);
  */
}

module.exports = {
  cameraFrameToBase64: cameraFrameToBase64,
  uint8ArrayToBase64: uint8ArrayToBase64,
  base64ToUint8Array: base64ToUint8Array,
  resizeImg: resizeImg
}